//
//  ZCComposePicView.swift
//  weibo
//
//  Created by Ning Xie on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let ZCComposePicViewCellIdentifier = "ZCComposePicViewCellIdentifier"

class ZCComposePicView: UICollectionView {

    var addImageButtonClosure:(()->())?
    
    var rightButtonItemEnableClosure:((enable:Bool)->())?
    
    //MARK: 懒加载控件
    var imageArray:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
    
        dataSource = self
        delegate = self
        self.hidden = true
        registerClass(ZCComposePicViewCell.self, forCellWithReuseIdentifier: ZCComposePicViewCellIdentifier)
    }
    
    //增加图片
    func addImage(image:UIImage){
        
        self.hidden = false
        rightButtonItemEnableClosure!(enable: true)
        if imageArray.count >= 9 {
            return
        }
        
        imageArray.append(image)
        self.reloadData()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemMargin: CGFloat = 5
        let itemWidth = (frame.size.width - 2 * itemMargin)/3
        
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
    }
}



extension ZCComposePicView: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if imageArray.count == 0 || imageArray.count == 9 {
            return imageArray.count
        }else{
            return imageArray.count + 1
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ZCComposePicViewCellIdentifier, forIndexPath: indexPath) as! ZCComposePicViewCell
        
        if indexPath.item == imageArray.count{
            cell.image = nil
        }else{
            
            cell.image = imageArray[indexPath.item]
            cell.deletePictureButtonClosure = { [weak self] in
                
                self?.imageArray.removeAtIndex(indexPath.item)
                
                if self?.imageArray.count == 0{
                    self?.hidden = true
                    self?.rightButtonItemEnableClosure!(enable: false)
                }
                
                self?.reloadData()

            }
        }
       
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if indexPath.item == imageArray.count
        {
            addImageButtonClosure!()
        }
    }
}



class ZCComposePicViewCell: UICollectionViewCell{
    
    var deletePictureButtonClosure:(()->())?
    
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        return view
    }()
    
    private lazy var deleteButton:UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setImage(UIImage(named:"compose_photo_close"), forState: .Normal)
        button.addTarget(self, action: #selector(ZCComposePicViewCell.deleteButtonAction(_:)), forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    var image:UIImage?{
        didSet{
            if image == nil {
                imageView.image = UIImage(named: "compose_pic_add_highlighted")
                self.deleteButton.hidden = true
            }else{
                
                imageView.image = image
                self.deleteButton.hidden = false
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        contentView.addSubview(imageView)
        contentView.addSubview(deleteButton)
        
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) in
            make.trailing.top.equalTo(imageView)
        }
    }
    @objc private func deleteButtonAction(button:UIButton){
        
        deletePictureButtonClosure!()
    }
}
